package com.im.app;

import com.im.app.listener.ClientListener;
import com.im.app.listener.GroupListener;
import com.im.app.listener.ServerListener;
import com.im.app.processor.ChatMessageProcessor;
import com.im.app.processor.JIMWsHandshakeProcessor;
import com.im.app.processor.LoginServiceProcessor;
import org.jim.core.packets.Command;
import org.jim.server.JimServer;
import org.jim.server.command.CommandManager;
import org.jim.server.command.handler.ChatReqHandler;
import org.jim.server.command.handler.HandshakeReqHandler;
import org.jim.server.command.handler.LoginReqHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.config.PropertyImServerConfigBuilder;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.im.app.mapper")
public class ChatImApplication {

    public static void main(String[] args) throws Exception {
//        SpringApplication.run(ChatImApplication.class, args);
        ImServerConfig config = new PropertyImServerConfigBuilder("jim.properties")
                .serverListener(new ServerListener()).build();
        config.setImGroupListener(new GroupListener());
        config.setImUserListener(new ClientListener());

        JimServer jimServer = new JimServer(config);

        //添加自定义握手处理器;
        HandshakeReqHandler handshakeReqHandler = CommandManager.getCommand(Command.COMMAND_HANDSHAKE_REQ, HandshakeReqHandler.class);
        handshakeReqHandler.addMultiProtocolProcessor(new JIMWsHandshakeProcessor());

        //添加登录业务处理器;
        LoginReqHandler loginReqHandler = CommandManager.getCommand(Command.COMMAND_LOGIN_REQ, LoginReqHandler.class);
        loginReqHandler.setSingleProcessor(new LoginServiceProcessor());

        //添加用户业务聊天记录处理器，用户自己继承抽象类BaseAsyncChatMessageProcessor即可，以下为内置默认的处理器！
        ChatReqHandler chatReqHandler = CommandManager.getCommand(Command.COMMAND_CHAT_REQ, ChatReqHandler.class);
        chatReqHandler.setSingleProcessor(new ChatMessageProcessor());

        jimServer.start();
    }



}
